package com.hnucm.cooklife.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import androidx.core.graphics.ColorUtils;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hnucm.cooklife.R;
import com.hnucm.cooklife.adapter.RecomAdapter;
import com.hnucm.cooklife.adapter.SearchResultAdapter;
import com.hnucm.cooklife.base.BaseActivity;
import com.hnucm.cooklife.base.Utils;
import com.hnucm.cooklife.base.utils.Constant;
import com.hnucm.cooklife.base.utils.LoginUtils;
import com.hnucm.cooklife.bean.db.Dish;
import com.hnucm.cooklife.contract.dishresult.Contract;
import com.hnucm.cooklife.presenter.dishresult.DishResultPresenter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

public class DishResultActivity extends BaseActivity<Contract.IDishResultView, DishResultPresenter> implements Contract.IDishResultView {
    private Context mContext;
    private SearchResultAdapter mResultAdapter;
    private List<Dish> mDishList = new ArrayList<>();
    private int currentPage = 1;

    private Map<String,String> dishname = new HashMap<>();

    @BindView(R.id.dish_result)
    RecyclerView mRecyclerView;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_dish_result;
    }

    private void initStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().setStatusBarColor(Utils.getColor(mContext));
        }
        if (ColorUtils.calculateLuminance(Utils.getColor(mContext)) >= 0.5) {
            // 设置状态栏中字体的颜色为黑色
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        } else {
            // 跟随系统
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        mContext = getApplicationContext();
        initAdapter();
        Intent intent = getIntent();
        String extra = intent.getStringExtra(Constant.KEY_KEYWORD);
        System.out.println(extra);
        dishname.put("key",intent.getStringExtra(Constant.KEY_KEYWORD));
        mPresenter.loadDishResult(currentPage,dishname);
        initStatusBar();
//        mSmartRefreshLayout.setNestedScrollingEnabled(false);
//        mSmartRefreshLayout.setOnLoadMoreListener(this);
//        mSmartRefreshLayout.setOnRefreshListener(this);
    }

    private void initAdapter() {
        LinearLayoutManager manager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(manager);
        mResultAdapter = new SearchResultAdapter(mContext,mDishList);
        mRecyclerView.setAdapter(mResultAdapter);
    }

    @Override
    protected DishResultPresenter createPresenter() {
        return new DishResultPresenter();
    }

    @Override
    public void onLoading() {

    }

    @Override
    public void onLoadingFailed() {

    }

    @Override
    public void onLoadSuccess() {

    }

    @Override
    public void onLoadDishResult(List<Dish> dishList) {
        mDishList.addAll(dishList);
        mResultAdapter.setDishList(mDishList);
    }

    @Override
    public void refreshDishResult(List<Dish> dishList) {
        mDishList.addAll(dishList);
        mResultAdapter.setDishList(mDishList);
    }
}
